using Godot;
using Newtonsoft.Json;
using System;

namespace Box;

public struct PTProject_JSON {
	public string project_name;
}

public partial class ProjectCreateWindow : FileDialog
{
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		FileSelected += _onCreateProject;
	}

	public void _onCreateProject(string path) {
		string project_name = this.CurrentFile.Substr(0,CurrentFile.Length - 7);
		string project_path = $"{CurrentDir}/{project_name}";
		string project_file_path = $"{project_path}/project.ptproj";
		
		DirAccess dir = DirAccess.Open(CurrentDir);
		dir.MakeDir(project_name);
		
		DirAccess project_dir = DirAccess.Open(project_path);
		project_dir.MakeDir("trees");
		project_dir.MakeDir("actions");
		project_dir.MakeDir("tasks");


		PTProject_JSON setting = new PTProject_JSON() {
			project_name = project_name
		};

		string json_str = JsonConvert.SerializeObject(setting);

		FileAccess file = FileAccess.Open(project_file_path,FileAccess.ModeFlags.WriteRead);
		file.StoreString(json_str);
		file.Close();
		

		ProcessTreeEditor.CurrentEditor.OpenProject(project_name,project_path);
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}
}
